package com.turnengine.client.local.unit.bean;

import com.turnengine.client.local.unit.enums.UnitMove;
import com.turnengine.client.local.unit.enums.UnitNode;
import com.turnengine.client.local.unit.enums.UnitType;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * The Unit.
 */
public class Unit implements IUnit, Comparable<IUnit> {

	/** The id. */
	private int id = -1;
	/** The name. */
	private String name = "";
	/** The node. */
	private UnitNode node = UnitNode.PARENT;
	/** The type. */
	private UnitType type = UnitType.PLAYER;
	/** The move. */
	private UnitMove move = UnitMove.NOT_MOVE;
	/** The group id. */
	private int groupId = -1;
	/** The faction id. */
	private int factionId = -1;

	/**
	 * Creates a new Unit.
	 */
	public Unit() {
	}

	/**
	 * Creates a new Unit.
	 * @param id the id
	 * @param name the name
	 * @param node the node
	 * @param type the type
	 * @param move the move
	 * @param groupId the group id
	 * @param factionId the faction id
	 */
	public Unit(int id, String name, UnitNode node, UnitType type, UnitMove move, int groupId, int factionId) {
		setId(id);
		setName(name);
		setNode(node);
		setType(type);
		setMove(move);
		setGroupId(groupId);
		setFactionId(factionId);
	}

	/**
	 * Creates a new Unit.
	 * @param unit the unit
	 */
	public Unit(Unit unit) {
		setId(unit.getId());
		setName(unit.getName());
		setNode(unit.getNode());
		setType(unit.getType());
		setMove(unit.getMove());
		setGroupId(unit.getGroupId());
		setFactionId(unit.getFactionId());
	}

	/**
	 * Creates a new Unit.
	 * @param iUnit the i unit
	 */
	public Unit(IUnit iUnit) {
		setId(iUnit.getId());
		setName(iUnit.getName());
		setNode(iUnit.getNode());
		setType(iUnit.getType());
		setMove(iUnit.getMove());
		setGroupId(iUnit.getGroupId());
		setFactionId(iUnit.getFactionId());
	}

	/**
	 * Returns the id.
	 * @return the id.
	 */
	public int getId() {
		return id;
	}

	/**
	 * Returns the name.
	 * @return the name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * Returns the node.
	 * @return the node.
	 */
	public UnitNode getNode() {
		return node;
	}

	/**
	 * Returns the type.
	 * @return the type.
	 */
	public UnitType getType() {
		return type;
	}

	/**
	 * Returns the move.
	 * @return the move.
	 */
	public UnitMove getMove() {
		return move;
	}

	/**
	 * Returns the group id.
	 * @return the group id.
	 */
	public int getGroupId() {
		return groupId;
	}

	/**
	 * Returns the faction id.
	 * @return the faction id.
	 */
	public int getFactionId() {
		return factionId;
	}

	/**
	 * Sets the id.
	 * @param id the id to set.
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * Sets the name.
	 * @param name the name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Sets the node.
	 * @param node the node to set.
	 */
	public void setNode(UnitNode node) {
		this.node = node;
	}

	/**
	 * Sets the type.
	 * @param type the type to set.
	 */
	public void setType(UnitType type) {
		this.type = type;
	}

	/**
	 * Sets the move.
	 * @param move the move to set.
	 */
	public void setMove(UnitMove move) {
		this.move = move;
	}

	/**
	 * Sets the group id.
	 * @param groupId the group id to set.
	 */
	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}

	/**
	 * Sets the faction id.
	 * @param factionId the faction id to set.
	 */
	public void setFactionId(int factionId) {
		this.factionId = factionId;
	}

	/**
	 * Returns the hash code.
	 * @return the hash code.
	 */
	@Override
	public int hashCode() {
		HashCodeBuilder hash = new HashCodeBuilder(1109, 37);
		hash.append(id);
		return hash.toHashCode();
	}

	/**
	 * Returns true if this is equal to the given object.
	 * @param object the object to compare.
	 * @return true if this is equal to the given object.
	 */
	@Override
	public boolean equals(Object object) {
		if (object == this) {
			return true;
		}
		if (object instanceof Unit) {
			Unit compare = (Unit) object;
			EqualsBuilder equals = new EqualsBuilder();
			equals.append(this.id, compare.id);
			return equals.isEquals();
		}
		return false;
	}

	/**
	 * Compare this to the given object.
	 * @param compare the object to compare to.
	 * @return the result of the comparison.
	 */
	@Override
	public int compareTo(IUnit compare) {
		CompareToBuilder builder = new CompareToBuilder();
		builder.append(this.id, compare.getId());
		return builder.toComparison();
	}

	/**
	 * Returns this as a string.
	 * @return this as a string.
	 */
	@Override
	public String toString() {
		ToStringBuilder string = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
		string.append("id", id);
		if (name != null) {
			string.append("name", name);
		}
		if (node != null) {
			string.append("node", node);
		}
		if (type != null) {
			string.append("type", type);
		}
		if (move != null) {
			string.append("move", move);
		}
		string.append("groupId", groupId);
		string.append("factionId", factionId);
		return string.toString();
	}
}
